package model

import (
	"ginblog/utils/errmsg"
	"time"
)

type View struct {
	User User `gorm:"foreignkey:Uid"`
	Uid     int    `json:"uid"`
	Aid     int    `json:"aid"`
	ID        uint `gorm:"primary_key" json:"id"`
	CreatedAt XTime `json:"created_at"`
	UpdatedAt XTime `json:"-"`
}

// 新增浏览
func CreateView(data *View)int  {
	err := db.Create(&data).Error
	if err !=nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

// 查询用户是否浏览了这篇文章 有值200
func IsViewArt(uid int,aid int)int {
	var view View
	db.Debug().Select("id").Where("uid = ? AND aid = ?", uid, aid).First(&view)
	if view.ID > 0  {
		return errmsg.SUCCSE
	}
	return errmsg.ERROR
}

// 更新文章
func SaveView(data *View) int {
	var view View
	var maps = make(map[string]interface{})

	maps["updated_at"] = time.Now().Format("2006-01-02 15:04:05")
	err = db.Debug().Model(&view).Where("uid = ? AND aid = ?", data.Uid, data.Aid).Update(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}